<?php
include_once('top.php');

if (!empty($_POST)) {
	$milestoneName = mysql_real_escape_string($_POST['name']);
	$desc = mysql_real_escape_string($_POST['description']);
	$due = mysql_real_escape_string($_POST['date']);
	$due = strtotime($due);
	$due =  date("Y-m-d",$due);
	$projectID = $_POST['project'];
	$_GET['milestone'] = $milestoneName;
	if (empty($milestoneName) || empty($_POST['date'])) $error='Cannot leave name or due date blank';
	elseif (isset($_POST['currMilestone'])) {
		$oldName = $_POST['currMilestone'];
		//echo "running update1</br>" . $milestoneName . "</br>" . $desc . "</br>" . $due . "</br>" . $projectID . "</br>Old name:" . $oldName . "</br>"; 
		if (isset($_POST['complete'])) {run("UPDATE MILESTONE SET `Name`='$milestoneName',`Description`='$desc',`Due_date`='$due',`Open`=false WHERE Project=$projectID AND Name='$oldName';");}
		else 						   {run("UPDATE MILESTONE SET `Name`='$milestoneName',`Description`='$desc',`Due_date`='$due',`Open`=true WHERE Project=$projectID AND Name='$oldName';");}
	} else {
		if (isset($_POST['complete'])) run("INSERT INTO MILESTONE (Name,Project,Description,Open,Due_Date) VALUES ('$milestoneName',$projectID,'$desc',false,'$due');");
		else run("INSERT INTO MILESTONE (Name,Project,Description,Open,Due_Date) VALUES ('$milestoneName',$projectID,'$desc',true,'$due');");
	}
} else {
	$projectID = $_GET['project'];
	$milestoneName = $_GET['name'] . $_GET['milestone']; // There is a discrepency between the different pages that link here
}
$project=fetch_only("SELECT Title, Description FROM PROJECT WHERE P_id=$projectID;");
$milestone=fetch_only("SELECT Name, Description, Due_date, Open FROM MILESTONE WHERE Project=$projectID AND Name='$milestoneName';");
$allTasks = fetch_all("SELECT Due_date, Open, Name, USER.First_name, USER.Last_name FROM TASK LEFT JOIN USER ON TASK.Employee=USER.Email WHERE TASK.Milestone='$milestoneName' AND TASK.Project=$projectID;");

$hasOpen = fetch_only("SELECT COUNT(`Name`) AS `Count` FROM `TASK` WHERE `Milestone`='$milestoneName' AND `Project`='$projectID' AND `Open`='1'");

$hasOpen = $hasOpen['Count'] > 0;

?>

<html>
<head>
	<title>GT Project Manager - Milestone Info</title>
	<link rel="stylesheet" type="text/css" href="main.css" />
	<link rel="stylesheet" type="text/css" href="inner.css" />
</head>
<body>
	<table cellspacing="0" cellpadding="0" id="main">
		<tr><td id="title">
			Milestone Info
			<div class="nav">
				<a href="login.php">Log Out</a><br />
				<a href="dashboard.php">Dashboard</a><br />
				<a href="project_milestones.php?project=<?php echo $projectID;?>">Project Milestones</a><br />
			</div>
		</td></tr>
		<tr>
			<td>
			<form action='milestone_info.php' method='POST' />
				<table class="details" cellspacing="0" cellpadding="0">
				<?php if (isset($error)) {
				echo '<tr class="message"><td colspan="2">' . $error . '</td></tr>';
				}?>
					<tr>
						<th>Project</th>
						<td><?php echo $project['Title'];?></td>
					</tr>
					<tr>
						<th>Name</th>
						<td><input type="text" name="name" value="<?php echo $milestone['Name']; ?>"/></td>
						<input type='hidden' name='project' value='<?php echo $projectID;?>'/>
						<?php if ($_GET['milestone']) echo "<input type='hidden' name='currMilestone' value='$milestoneName' />";?>
					</tr>
					<tr>
						<th>Description</th>
						<td>
							<textarea type="text" name="description" resizable="false"><?php echo $milestone['Description'];?></textarea>
						</td>
					</tr>
					<tr>
						<th>Due Date</th>
						<td class="date">
							<?php
							if (!empty($milestone['Name'])) {
							?>
							<input type="text" name="date" value="<?php echo date("n/j/Y",strtotime($milestone['Due_date']))?>" />
							<?php
							} else {
							?>
							<input type="text" name="date" value="" />
							<?php
							}
							?>
						</td>
					</tr>
					<tr>
						<th>Complete?</th>
						<?php
						if (!$hasOpen) {
						?>
						<td><input type="checkbox" name="complete" <? if (!$milestone['Open']) echo "checked='true'";?> /></td>
						<?php
						} else {
						?>
						<td>All tasks must be closed to mark the milestone complete</td>
						<?php
						}
						?>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<table class="subsection summary" cellspacing="0" cellpadding="0">
					<tr><td class="title" colspan="4">
						Tasks
						<hr size="1" />
						<div class="link">
							<a href="task_info.php?<?php echo "project=$projectID&milestone=$milestoneName";?>">New Task</a>
						</div>
					</td></tr>
					<?php 
					if (count($allTasks) > 0) {
					?>
					<tr>
						<th>Status</th>
						<th>Name</th>
						<th>Assigned To</th>
					</tr>
					<?php
					for ($i=0;$i<count($allTasks);$i++) {
						if (!$allTasks[$i]['Open']){
						echo "<tr><td>Closed</td>";
						echo "<td><a href='task_info.php?project=$projectID&milestone=$milestoneName&name=" . $allTasks[$i]['Name'] . "'>" . $allTasks[$i]['Name'] . "</a></td>";
						echo "<td>" . $allTasks[$i]['First_name'] . " " . $allTasks[$i]['Last_name'] . "</td></tr>";
						}
					}
					?>
					<?php
					for ($i=0;$i<count($allTasks);$i++) {
						if ($allTasks[$i]['Open']){
						if (strtotime($allTasks[$i]['Due_date'])<time() && isset($allTasks[$i]['Due_date']) && $allTasks[$i]['Open']) echo "<tr class='late'><td>Overdue since " . date("n/j/Y",strtotime($allTasks[$i]['Due_date'])) . "</td>";
						elseif (isset($allTasks[$i]['Due_date']) && $allTasks[$i]['Open']) echo "<tr><td>Due " . date("n/j/Y",strtotime($allTasks[$i]['Due_date'])) . "</td>";
						elseif (isset($allTasks[$i]['Due_date'])) echo "<tr><td>Completed</td>";
						echo "<td><a href='task_info.php?project=$projectID&milestone=$milestoneName&name=" . $allTasks[$i]['Name'] . "'>" . $allTasks[$i]['Name'] . "</a></td>";
						echo "<td>" . $allTasks[$i]['First_name'] . " " . $allTasks[$i]['Last_name'] . "</td></tr>";
						}
					}
					}
					?>
				</table>
			</td>
		</tr>
		<tr>
			<td class="button_bar">
				<input type="submit" value="Save" />
			</td>
		</tr>
	</table>
	</form>
</body>
</html>